<?php
/**
 * 轻舟CMS(QZLOG)
 *
 * 一款基于PHP+MYSQL的开源内容管理系统，模板标签简单、可扩展性强，利于二次开发，轻松建站的首选利器。
 *
 * @package     QZLOG
 * @license     http://www.qzlog.com/license
 * @link        http://www.qzlog.com
 * @mail        info@shyohan.com
 * @since       Version 3.0
 * @copyright   Copyright (c) 2015 - 2017 上海优翰信息科技有限公司. All rights reserved.
 */

defined('IN_QZLOG') OR exit('QZLOG:No direct script access allowed!');

/**
 * 属性控制器
 * @author LINK <link@shyohan.com>
 */
class AttributeController extends QzController
{

    private $db;

    public function __init()
    {
        $this->db = K("Attribute");
    }

    /**
     * 列表
     * @author LINK <link@shyohan.com>
     */
    public function index()
    {
        $model_id = Q('model_id');
        $count = $this->db->where(array('model_id' => $model_id))->count();
        $page = new Page($count);
        $this->page = $page->show();
        $data = $this->db->where(array('model_id' => $model_id))->order("id DESC")->limit($page->limit())->all();
        foreach ($data as $key => $value) {
            $data[$key]['type'] = get_attribute_type($value['type']);
        }
        int_to_string($data, array('status' => array(1 => '正常', 0 => '禁用')));
        $bread = make_bread(array('模型管理' => '', '字段管理' => '', '字段列表' => ''));
        $this->assign('bread', $bread);
        $this->assign('data', $data);
        $this->assign('model_id', $model_id);
        $this->display();
    }

    /**
     * 新增字段||表
     * @author LINK <link@shyohan.com>
     */
    public function add()
    {
        if (IS_POST) {
            if ($this->db->addAttribute()) {
                qz_ajax_return('新增成功');
            } else {
                qz_ajax_return_error($this->db->error);
            }
        } else {
            $model_id = Q('model_id');
            $model = M('model')->field('title,name,field_group')->find($model_id);
            $get_attribute_type = get_attribute_type();
            $this->assign('attribute_type', $get_attribute_type);
            $this->assign('model', $model);
            $this->assign('info', array('model_id' => $model_id));
            $this->display();
        }
    }

    /**
     * 编辑
     * @author LINK <link@shyohan.com>
     */
    public function edit()
    {
        if (IS_POST) {
            if ($this->db->editAttribute()) {
                qz_ajax_return('编辑成功');
            } else {
                qz_ajax_return_error($this->db->error);
            }
        } else {
            $id = Q('id');
            if (empty($id)) {
                qz_ajax_return_error('参数不能为空！');
            }
            $data = $this->db->find($id);
            $model = M('model')->field('title,name,field_group')->find($data['model_id']);
            $get_attribute_type = get_attribute_type();
            $this->assign('attribute_type', $get_attribute_type);
            $this->assign('model', $model);
            $this->assign('info', $data);
            $this->display();
        }
    }

    /**
     * 删除
     * @author LINK <link@shyohan.com>
     */
    public function del()
    {
        if ($this->db->delAttribute()) {
            qz_ajax_return("删除成功!");
        } else {
            qz_ajax_return_error($this->db->error);
        }
    }
}
